var WebSocketServer = require('ws').Server,
    wss = new WebSocketServer({ port: 8180 });

var clientNum = 0;
var clientArr = [];
var bottomArr =[];
var nameArr = [];
var passArr= [];
var player;
var lastplayer;
var theFirstPlayer = true;


//客户端建立连接时触发的事件
wss.on('connection', function (ws) {
    clientNum++;
    console.log(clientNum);
    ws.on('message', function (message) {

        message = JSON.parse(message);
        nameArr.push(message.user);
        clientArr.push({user:message.user,func:"sendPoker",poker:[],all:[]});
        if(clientNum == 3){
            var poker = [];
            var poker_value = [3,4,5,6,7,8,9,10,'J','Q','K','A',2];
            for(var i= 3; i < 16; i++) {
                for(var j = 1; j < 5; j++){
                    var json = {};
                    json.num = i;
                    json.color = j;
                    json.value = poker_value[i-3];
                    json.src = "image/poker_" + i + "_" + j + ".jpg";
                    poker.push(json);
                }
            }
            poker.push({num:16, vaule:'sk' ,color:5 , src:'image/poker_16_5.jpg'}, {num:17, vaule:'bk', color:5, src:'image/poker_17_5.jpg'});
            poker.sort(function(){return Math.random() - 0.5});
            for (var i = 0;i < 3;i++){
                clientArr[i].poker = poker.splice(0,17);
                clientArr[i].all = nameArr;
            }
            bottomArr = poker;
            wss.clients.forEach(function (client) {
                client.send(JSON.stringify(clientArr[client._ultron.id - 1]));
                 console.log(clientArr[client._ultron.id - 1])
            });
            clientNum++;
        }

    });



    //接收到客户端消息时触发的事件
    ws.on('message', function (message) {

        message = JSON.parse(message);

        switch (message.msg.func){

            case "QDZ":
                //抢地主的业务逻辑处理
                //收底
                if (theFirstPlayer){
                    for (var i = 0; i < clientArr.length;i++){
                        if (clientArr[i].user == message.user){
                            clientArr[i].poker = clientArr[i].poker.concat(bottomArr);
                            clientArr[i].func = "qiangdizhu";
                            wss.clients.forEach(function (client) {
                                if (client._ultron.id - 1 == i){
                                    client.send(JSON.stringify(clientArr[i]));
                                }
                            });
                            break;
                        }
                    }
                    wss.clients.forEach(function (client) {

                            client.send(JSON.stringify({bottom:bottomArr,func:"bottom"}));

                    });
                    theFirstPlayer = false;
                }
                break;

            case "CP":
                //判断出牌是否合理
                //出牌合理,则广播
                wss.clients.forEach(function (client) {
                    passArr = message.msg.cont;
                    player = message.user;
                    console.log(player);
                    client.send(JSON.stringify({user:player,func:"chupai",poker:passArr}));

                });
                break;
            case "WIN":
                wss.clients.forEach(function (client) {

                    client.send(JSON.stringify({user:message.user,func:"victory"}));

                });

                break;

            case "GP":
                wss.clients.forEach(function (client) {
                    lastplayer = message.user;
                    client.send(JSON.stringify({user:[player,lastplayer],func:"guopai",poker:passArr}));

                });
                break;

        }




    });
});